#include "MainWindow.h"
#include "../Core/Exceptions.h"

#include <QHBoxLayout>
#include <QScrollArea>

#include <iostream>

using namespace std;


namespace cagd
{
    MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
    {
        setupUi(this);

        try
        {
            // create your widgets
            _gl_widget = new GLWidget(this);

            _settings_widget = new SettingsWidget(this);

            // create a scroll area
            QScrollArea* scroll_area = new QScrollArea;

            // make the _settings_widget scrollable
            scroll_area->setWidget(_settings_widget);

            // copy the size policy of _settings_widget
            scroll_area->setSizePolicy(_settings_widget->sizePolicy());

            /*
              define the layout of the main window's central widget

              |--------------------------------------|
              |    centralWidget()                   |
              |    |------------|------------------| |
              |    |            | scroll_area      | |
              |    | _gl_widget | |--------------| | |
              |    |            | | _side_widget | | |
              |    |            | |              | | |
              |    |            | |--------------| | |
              |    |------------|------------------| |
              |                                      |
              |--------------------------------------|

            */

            centralWidget()->setLayout(new QHBoxLayout());

            centralWidget()->layout()->addWidget(_gl_widget);
            centralWidget()->layout()->addWidget(scroll_area);

            // provide signal and slot mechanism by defining connections
            connect(_settings_widget->rotate_x_slider, SIGNAL(valueChanged(int)), _gl_widget, SLOT(set_angle_x(int)));
            connect(_settings_widget->rotate_y_slider, SIGNAL(valueChanged(int)), _gl_widget, SLOT(set_angle_y(int)));
            connect(_settings_widget->rotate_z_slider, SIGNAL(valueChanged(int)), _gl_widget, SLOT(set_angle_z(int)));

            connect(_settings_widget->trans_x_spin_box, SIGNAL(valueChanged(double)), _gl_widget, SLOT(set_trans_x(double)));
            connect(_settings_widget->trans_y_spin_box, SIGNAL(valueChanged(double)), _gl_widget, SLOT(set_trans_y(double)));
            connect(_settings_widget->trans_z_spin_box, SIGNAL(valueChanged(double)), _gl_widget, SLOT(set_trans_z(double)));

            connect(_settings_widget->zoom_spin_box, SIGNAL(valueChanged(double)), _gl_widget, SLOT(set_zoom_factor(double)));

            connect(_settings_widget->reset_to_original_view_button, SIGNAL(clicked()), _gl_widget, SLOT(reset_to_original_view()));

            connect(_settings_widget->evolve, SIGNAL(stateChanged(int)), _gl_widget, SLOT(evolve(int)));

            connect(_settings_widget->random_initialization_button, SIGNAL(clicked()), _gl_widget, SLOT(reset_initialization()));

            // parameters
            connect(_settings_widget->alpha_spin_box, SIGNAL(valueChanged(double)), _gl_widget, SLOT(set_alpha(double)));
            connect(_settings_widget->beta_spin_box, SIGNAL(valueChanged(double)), _gl_widget, SLOT(set_beta(double)));

            connect(_settings_widget->data_point_size_u_spin_box, SIGNAL(valueChanged(int)), _gl_widget, SLOT(set_data_point_size_u(int)));
            connect(_settings_widget->data_point_size_v_spin_box, SIGNAL(valueChanged(int)), _gl_widget, SLOT(set_data_point_size_v(int)));

            connect(_settings_widget->individual_number_spin_box, SIGNAL(valueChanged(int)), _gl_widget, SLOT(set_individual_number(int)));

            connect(_settings_widget->individual_number_to_show_spin_box, SIGNAL(valueChanged(int)), _gl_widget, SLOT(set_individual_number_to_show(int)));

            connect(_settings_widget->data_point_element_u_combo_box, SIGNAL(currentIndexChanged(int)), _gl_widget, SLOT(set_data_point_elements_u(int)));
            connect(_settings_widget->data_point_element_v_combo_box, SIGNAL(currentIndexChanged(int)), _gl_widget, SLOT(set_data_point_elements_v(int)));

            connect(_settings_widget->data_point_element_x_spin_box, SIGNAL(valueChanged(double)), _gl_widget, SLOT(set_data_point_element_x(double)));

            connect(_settings_widget->data_point_element_y_spin_box, SIGNAL(valueChanged(double)), _gl_widget, SLOT(set_data_point_element_y(double)));

            connect(_settings_widget->data_point_element_z_spin_box, SIGNAL(valueChanged(double)), _gl_widget, SLOT(set_data_point_element_z(double)));

            // save / load
            connect(_settings_widget->save_button, SIGNAL(clicked()), _gl_widget, SLOT(save_to_file()));
            connect(_settings_widget->load_button, SIGNAL(clicked()), _gl_widget, SLOT(load_from_file()));
        }
        catch (Exception &e)
        {
            cout << e << endl;
        }
    }
}

void cagd::MainWindow::on_actionE_xit_triggered()
{
    qApp->exit(0);
}
